home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6227 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  78 lines

  1. Path: rcp6.elan.af.mil!rscernix!danpop
  2. From: danpop@mail.cern.ch (Dan Pop)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: sscanf bug??????
  5. Date: 23 Feb 96 01:17:57 GMT
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <danpop.825038277@rscernix>
  8. References: <4fimvo$82s@fnord.dfw.net> <10FEB199622213548@erich.triumf.ca> <824160515snz@genesis.demon.co.uk> <4gh6uf$i2@mailhub.scitec.com.au> <06R9B8G@gellbo.gellrich-gmbh.de>
  9. NNTP-Posting-Host: ues5.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11.  
  12. In <06R9B8G@gellbo.gellrich-gmbh.de> wolfi@gellbo.gellrich-gmbh.de (Wolfgang Formann) writes:
  13.  
  14. >In <4gh6uf$i2@mailhub.scitec.com.au> ramsesy@rd.scitec.com.au (Ramses Youhana) writes:
  15. >
  16. >>You need the %ld for 16 bit processors or processors running in 16 bit mode.
  17. >>Unfortunately, your stuck with this when using a PC (using 16 bit mode).
  18. >>With 32 bit processors (such as a M68302 micro-controller) you don't need
  19. >>the %ld.  
  20.  
  21. BULLSHIT!!!
  22.  
  23. >>%d will do as the processor treats the number as a 32 bit entity
  24. >>anyway.  You have to be careful when saying that %ld and %d are interchangeable,
  25.  
  26. Even better, never say such nonsense.  %d and %ld aren't and have never
  27. been interchangeable.  That is, not in C.
  28.  
  29. >>as a compiler for a 32 bit processor (such as the M68302) may not support the
  30. >>"l" in the %ld format.
  31.  
  32. More bullshit.  %ld is a standard feature of K&R and ANSI C.  A compiler
  33. which doesn't support it is not a C compiler.
  34.  
  35. >Well until DEC's Alpha was released I thought the same. I did not write the
  36. >'l' in '%ld'. But then I had to port some of my code to this 64-bit
  37. >architecture. It was horrible!
  38.  
  39. You got exactly what you deserved.  You've learned the hard way the
  40. difference between correct code and broken code that happens to "work"
  41. on a certain platform with a certain compiler.
  42.  
  43. >So, if you plan to use your programs *ONLY* on 32-bit processors, then there
  44. >is no need to write the 'l', but I am sure the 64-bitters will come sooner
  45. >as we all do think and then the 'l' will be a *MUST*.
  46.  
  47. The 'l' has _always_ been a must.  Using %d for a long variable invokes
  48. undefined behaviour and it is plain silly to make _any_ kind of speculations
  49. about the behaviour of a program invoking undefined behaviour.
  50.  
  51. BTW, even if int and long are the same size, a smart compiler can detect
  52. and complain about code which uses %d and %ld "interchangeably":
  53.  
  54.     ues5:~/tmp 575> cat test.c
  55.     #include <stdio.h>
  56.  
  57.     int main()
  58.     {
  59.     int i;
  60.     long l;
  61.  
  62.     scanf("%d %ld", &l, &i);
  63.     return printf("%d %ld\n", l, i);
  64.     }
  65.     ues5:~/tmp 576> gcc -Wall test.c
  66.     test.c: In function `main':
  67.     test.c:8: warning: int format, different type arg (arg 2)
  68.     test.c:8: warning: long int format, different type arg (arg 3)
  69.     test.c:9: warning: int format, different type arg (arg 2)
  70.     test.c:9: warning: long int format, different type arg (arg 3)
  71.  
  72. Dan
  73. --
  74. Dan Pop
  75. CERN, CN Division
  76. Email: danpop@mail.cern.ch 
  77. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  78.